package cfca.paperless.client.servlet.customer;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Date;
import org.apache.commons.io.FileUtils;

import cfca.paperless.client.bean.ChannelBean;
import cfca.paperless.client.bean.PdfBean;
import cfca.paperless.client.bean.SealUserInfo;
import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.util.Base64;
import cfca.paperless.client.util.GUID;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StrategyUtil;

/**
 * 北京捷越定制的批量容错复合签章接口
 * @Description 
 * @Author dell
 * @Date 2017-10-16
 * @CodeReviewer TODO
 */
public class testCompoundSealAutoPdfWithoutInterrupt {

    public static void main(String[] args) throws Exception {

        try {
            PaperlessClient clientBean = new PaperlessClient(PaperlessConfig.url, 10000, 60000);

            // /////////////////////////如下为加盖印章的策略/////////////////////////////////////////////////
            String sealCode1 = "org001";
            String sealPassword1 = "11111111";
            SealUserInfo sealUserInfo1 = new SealUserInfo("签章者1", "北京1", "签合同1");
            SignLocation signLocation1 = new SignLocation("银行签章1", "D", 20, 20);
            String sealStrategyXml1 = StrategyUtil.createSealStrategyXml(sealCode1, sealPassword1, sealUserInfo1, signLocation1);
            System.out.println(sealStrategyXml1);
            // /////////////////////////以上为加盖印章的策略//////////////////////////////////////////////

            // /////////////////////////如下为加盖印章的策略/////////////////////////////////////////////////
            String sealCode2 = "cert092";
            String sealPassword2 = "11111111";
            SealUserInfo sealUserInfo2 = new SealUserInfo("签章者2", "北京2", "签合同2");
            SignLocation signLocation2 = new SignLocation("银行签章", "D", 20, 20);
            String sealStrategyXml2 = StrategyUtil.createSealStrategyXml(sealCode2, sealPassword2, sealUserInfo2, signLocation2);
            System.out.println(sealStrategyXml2);
            // /////////////////////////以上为加盖印章的策略//////////////////////////////////////////////

            String compoundSealStrategyXml = "<List>" + sealStrategyXml1 + sealStrategyXml2 + "</List>";

            String bizSerialNo = GUID.generateId();// 业务流水号

            String bizTypeCode = "B001";// 业务类型，比如：开户、开通网银
            
            byte[] pdfFile = FileUtils.readFileToByteArray(new File("./TestData/autoCalculatePdfHash.pdf"));
            PdfBean pdfBean = new PdfBean(Base64.toBase64String(pdfFile), bizSerialNo, bizTypeCode, "");
            pdfBean.setReturnPdfOrNot(true);
            String pdfBeanXml = PaperlessUtil.buildPdfBeanXml(pdfBean);
            
            long start = System.currentTimeMillis();
            String channelBeanXml = new ChannelBean(PaperlessConfig.channelCode).toString();
            System.out.println(channelBeanXml);
            
            byte[] sealedPdf = clientBean.compoundSealAutoPdfWithoutInterrupt(pdfBeanXml, "", compoundSealStrategyXml, PaperlessConfig.operatorCode, channelBeanXml);

            System.out.println(sealedPdf.length);
            String errorRsString = PaperlessUtil.isError(sealedPdf);
            if ("".equals(errorRsString)) {
                IoUtil.write("./TestData/output/compoundSealAutoPdf.pdf", sealedPdf);
            } else {
                System.out.println(new String(sealedPdf, "UTF-8"));
            }
            System.out.println("########## compoundSealAutoPdf" + " end,time:" + (System.currentTimeMillis() - start) + "##########");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
